﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using System.Xml;
using OpenQA.Selenium;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Support.UI;
using Ro.AssistFunction.BackNodeInfo;
using Ro.AssistFunction.OutputManage;
using Ro.Common.RoArgs;
using Ro.Common.RoInterface.RoWebAction;
using Ro.Common.RoInterface.UserType;
using Ro.DriverEvent.FindElement;

namespace Ro.DriverEvent.WebAction
{
    /// <summary>
    /// 元素操作
    /// </summary>
    public class WebRoWebElement : IWebRoWebElementFunc
    {
        /// <summary>
        /// 控件清除操作
        /// </summary>
        /// <param name="sigNode">清除操作</param>
        /// <returns>返回是否完成</returns>
        public WebActionResult WebRoWebElementClear(XmlNode sigNode)
        {
            //返回值
            WebActionResult back;
            ExtractNeedInfo extractNeedInfo = new ExtractNeedInfo();
            //提取超时
            TimeSpan timeout = extractNeedInfo.ExtractTimeout(sigNode);

            //提取元素
            string value = extractNeedInfo.ExtractAttributeValue(sigNode, "web:RoWebElementID");
            FindWebElement findWebElement = new FindWebElement(value, timeout);

            //获取webelement
            IWebElement webElement = findWebElement.WebElement;

            try
            {
                if (webElement == null)
                {
                    back = BackActionResult.BackWebResult(false, value, "WebRoWebElementClear操作遭遇查找元素失败，获取元素为null"); //结果
                }
                else
                {
                    webElement.Clear();
                    back = BackActionResult.BackWebResult(true, value, "没有附加信息"); //结果
                }
            }
            // 抛出异常
            catch (Exception e)
            {
                back = BackActionResult.BackWebResult(false, value, "WebRoWebElementClear控件操作遭遇System异常"); //结果
                ExceptionOutPut.SystemExceptionOut("WebRoWebElementClear", e);
            }
            return back;
        }


        /// <summary>
        /// 控件点击操作
        /// </summary>
        /// <param name="sigNode">点击操作</param>
        /// <returns>返回是否完成</returns>
        public WebActionResult WebRoWebElementClick(XmlNode sigNode)
        {
            //返回值
            WebActionResult back;
            ExtractNeedInfo extractNeedInfo = new ExtractNeedInfo();
            //提取超时
            TimeSpan timeout = extractNeedInfo.ExtractTimeout(sigNode);

            //提取元素
            string value = extractNeedInfo.ExtractAttributeValue(sigNode, "web:RoWebElementID");
            FindWebElement findWebElement = new FindWebElement(value, timeout);
            //获取webelement
            IWebElement webElement = findWebElement.WebElement;

            try
            {
                if (webElement == null)
                {
                    back = BackActionResult.BackWebResult(false, value, "WebRoWebElementClick，获取元素为null"); //结果
                }
                else
                {
                    Actions actions= new Actions(RoTestObject.WebTestObject);
                    actions.Click(webElement).Perform();
                    //webElement.Click();
                    back = BackActionResult.BackWebResult(true, value, "没有附加信息"); //结果
                }
            }
            // 抛出异常
            catch (Exception e)
            {
                back = BackActionResult.BackWebResult(false, value, "WebRoWebElementClick控件操作遭遇System异常"); //结果
                ExceptionOutPut.SystemExceptionOut("WebRoWebElementClick", e);
            }
            return back;
        }


        /// <summary>
        /// 控件选择操作（下拉框）
        /// </summary>
        /// <param name="sigNode">下拉框选择操作</param>
        /// <returns>返回是否完成</returns>
        public WebActionResult WebRoWebElementSelect(XmlNode sigNode)
        {
            //返回值
            WebActionResult back;
            ExtractNeedInfo extractNeedInfo = new ExtractNeedInfo();
            //提取超时
            TimeSpan timeout = extractNeedInfo.ExtractTimeout(sigNode);

            //提取元素
            string value = extractNeedInfo.ExtractAttributeValue(sigNode, "web:RoWebElementID");
            FindWebElement findWebElement = new FindWebElement(value, timeout);

            //获取webelement
            IWebElement webElement = findWebElement.WebElement;

            //下拉值
            string sendvalue = extractNeedInfo.ExtractInnerText(sigNode, "web:Value");

            //转换为正常的输入值
            ArgsIntoValue argsIntoValue = new ArgsIntoValue(sendvalue);
            sendvalue = argsIntoValue.BackValue;

            //提取类型
            string typevalue = extractNeedInfo.ExtractAttributeValue(sigNode, "web:SelectType");

            try
            {
                if (webElement == null)
                {
                    back = BackActionResult.BackWebResult(false, value, "WebRoWebElementSelect操作遭遇查找元素失败，获取元素为null"); //结果
                }
                else
                {
                    //更新EasyUI的下拉框选项做法
                    if (webElement.TagName == "select")
                    {
                        //下拉菜单选项  
                        SelectElement sel = new SelectElement(webElement);

                        switch (typevalue)
                        {
                            case "ByIndex":
                                sel.SelectByIndex(Convert.ToInt32(sendvalue));
                                break;

                            case "ByValue":
                                sel.SelectByValue(sendvalue);
                                break;

                            case "ByText":
                                sel.SelectByText(sendvalue);
                                break;
                        }

                        back = BackActionResult.BackWebResult(true, value, "没有附加信息"); //结果
                    }
                    //假定遇到的target不是select,估计就是EasyUI的下拉框选择了，而下拉框的控件并不绑定
                    else
                    {
                        //Debug
                        string xpath = "//div[@class=\"panel combo-p\"]";
                        ReadOnlyCollection<IWebElement> getobj = RoTestObject.WebTestObject.FindElements(By.XPath(xpath));
                        IWebElement tempdiv = (from temp in getobj where temp.GetAttribute("outerHTML").Contains("display: block") select temp).SingleOrDefault();

                        //判断是否存在
                        if (tempdiv == null)
                        {
                            back = BackActionResult.BackWebResult(true, value, "没有附加信息"); //结果
                        }
                        else
                        {
                            //控件存在，按照给定的text值进行点击选择
                            ReadOnlyCollection<IWebElement> tempdivs = tempdiv.FindElements(By.XPath("/div/descendant::div"));
                            IWebElement ele = (from temp in tempdivs where temp.Text == sendvalue select temp).SingleOrDefault();
                            if (ele == null)
                            {
                                back = BackActionResult.BackWebResult(false, value, "控件为非Select类型"); //结果
                                back.ExtraInfo = "控件为非Select类型";
                            }
                            else
                            {
                                ele.Click();
                                back = BackActionResult.BackWebResult(true, value, "控件为非Select类型"); //结果
                                back.ExtraInfo = "控件为非Select类型";
                            }
                        }
                    }
                }
            }
            // 抛出异常
            catch (Exception e)
            {
                back = BackActionResult.BackWebResult(false, value, "WebRoWebElementSelect控件操作遭遇System异常"); //结果
                ExceptionOutPut.SystemExceptionOut("WebRoWebElementSelect", e);
            }
            return back;
        }


        /// <summary>
        /// 控件输入值
        /// </summary>
        /// <param name="sigNode">输入操作</param>
        /// <returns>返回是否完成</returns>
        public WebActionResult WebRoWebElementSendKeys(XmlNode sigNode)
        {
            //返回值
            WebActionResult back;
            ExtractNeedInfo extractNeedInfo = new ExtractNeedInfo();
            //提取超时
            TimeSpan timeout = extractNeedInfo.ExtractTimeout(sigNode);

            //提取元素
            string value = extractNeedInfo.ExtractAttributeValue(sigNode, "web:RoWebElementID");
            //初始化构造函数
            FindWebElement findWebElement = new FindWebElement(value, timeout);
            IWebElement ele = findWebElement.WebElement;
            try
            {
                if (ele == null)
                {
                    back = BackActionResult.BackWebResult(false, value, "WebRoWebElementSendKeys操作遭遇查找元素失败，获取元素为null"); //结果
                }
                else
                {
                    //输入值
                    string sendvalue = extractNeedInfo.ExtractInnerText(sigNode, "web:Value");

                    //转换为正常的输入值
                    ArgsIntoValue argsIntoValue = new ArgsIntoValue(sendvalue);
                    sendvalue = argsIntoValue.BackValue;

                    if (Convert.ToBoolean(extractNeedInfo.ExtractAttributeValue(sigNode, "web:ClearFirst")))
                    {
                        ele.Clear();
                        Thread.Sleep(100);
                    }
                    ele.SendKeys(sendvalue);
                    back = BackActionResult.BackWebResult(true, value, "没有附加信息"); //结果
                }
            }
            // 抛出异常
            catch (Exception e)
            {
                back = BackActionResult.BackWebResult(false, value, "WebRoWebElementSendKeys控件操作遭遇System异常"); //结果
                ExceptionOutPut.SystemExceptionOut("WebRoWebElementSendKeys", e);
            }
            return back;
        }


        /// <summary>
        /// 控件焦点操作
        /// </summary>
        /// <param name="sigNode">对应节点</param>
        /// <returns>返回操作</returns>
        public WebActionResult WebRoWebElementFocus(XmlNode sigNode)
        {
            //返回值
            WebActionResult back;
            ExtractNeedInfo extractNeedInfo = new ExtractNeedInfo();
            //提取超时
            TimeSpan timeout = extractNeedInfo.ExtractTimeout(sigNode);

            //提取元素
            string value = extractNeedInfo.ExtractAttributeValue(sigNode, "web:RoWebElementID");
            FindWebElement findWebElement = new FindWebElement(value, timeout);

            //获取webelement
            IWebElement ele = findWebElement.WebElement;

            //提取是否给定焦点
            bool focus = Convert.ToBoolean(extractNeedInfo.ExtractAttributeValue(sigNode, "web:SetFocus"));

            try
            {
                if (ele == null)
                {
                    back = BackActionResult.BackWebResult(false, value, "WebRoWebElementFocus操作遭遇查找元素失败，获取元素为null"); //结果
                }
                else
                {
                    if (!focus)
                    {
                        IWebElement tempElement = RoTestObject.WebTestObject.FindElement(By.XPath("../.."));
                        Actions action = new Actions(RoTestObject.WebTestObject);
                        action.Click(tempElement).Perform();
                        Thread.Sleep(100);
                        action.SendKeys(Keys.Escape).Perform();
                        Thread.Sleep(100);
                    }

                    ele.Click();
                    back = BackActionResult.BackWebResult(true, value, "没有附加信息"); //结果
                }
            }
            // 抛出异常
            catch (Exception e)
            {
                back = BackActionResult.BackWebResult(false, value, "WebRoWebElementFocus控件操作遭遇System异常"); //结果
                ExceptionOutPut.SystemExceptionOut("WebRoWebElementFocus", e);
            }
            return back;
        }
    }
}